package com.ivsign.lib_idcardidentification;

import android.app.Application;
import android.content.Context;
import android.os.HandlerThread;
import android.os.Process;
import android.widget.Toast;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.scan.BleScanRuleConfig;

/**
 * @description :
 * @autHor :  Jason
 * @date : 2018/8/15 19:51
 */

public class BLEBlueToothAdapter implements IBlueToothAdapter {


    private IDCardController idCardController;
    private Context context;
    private BleDevice bleDevice;


    @Override
    public void init(Application context) {
        this.context =context;
        BleManager.getInstance().init(context);
        BleManager.getInstance()
                .enableLog(true)
                .setReConnectCount(1, 5000)
                .setSplitWriteNum(30)
                .setConnectOverTime(10000)
                .setOperateTimeout(5000);
    }

    /**
     *    扫描指定的Service
     * @param callback
     */
    @Override
    public void scan(BleScanCallback callback) {

        if (!BleManager.getInstance().isBlueEnable()) {

            //BleManager.getInstance().enableBluetooth();
            callback.onScanFinished(null);
            Toast.makeText(context, "请先开启蓝牙", Toast.LENGTH_LONG).show();
            return;
        }

        BleScanRuleConfig.Builder builder = new BleScanRuleConfig.Builder();

//        if (uuids != null) {
//            builder.setServiceUuids(uuids); // 只扫描指定的服务的设备，可选
//        }

//                    .setDeviceName(true, names)         // 只扫描指定广播名的设备，可选
//                    .setDeviceMac(mac)                  // 只扫描指定mac的设备，可选
        builder.setAutoConnect(true)      // 连接时的autoConnect参数，可选，默认false
                .setScanTimeOut(10000)              // 扫描超时时间，可选，默认10秒；小于等于0表示不限制扫描时间
                .build();
        BleScanRuleConfig scanRuleConfig = builder.build();
        BleManager.getInstance().initScanRule(scanRuleConfig);
        BleManager.getInstance().scan(callback);

    }

    /**
     * connect a device through its mac without scan,whether or not it has been connected
     *
     * @param mac
     * @param bleGattCallback
     * @return
     */
    @Override
    public void connect(String mac, BleGattCallback bleGattCallback) {
        if (!BleManager.getInstance().isBlueEnable()) {
            Toast.makeText(context, "请先开启蓝牙", Toast.LENGTH_LONG).show();
            return;
        }
        BleManager.getInstance().connect(mac, bleGattCallback);
    }


    /**
     * work on mainThread
     *
     * @param bleDevice
     * @param uuid
     * @param mIReadListener
     * @param context
     */
    @Override
    public void readId(BleDevice bleDevice, DeviceUUID uuid, IReadListener mIReadListener, Context context) {
        HandlerThread handlerThread = new HandlerThread("BLESubThread", Process.THREAD_PRIORITY_BACKGROUND);
        handlerThread.start();//必须开启
        //
        idCardController = new IDCardController(handlerThread.getLooper(), bleDevice, uuid, mIReadListener, context);
        idCardController.readID();
        this.bleDevice =bleDevice;
    }

    @Override
    public void reset() {
        if (idCardController!=null){
            idCardController.reset();
        }else {
            throw new NullPointerException("please readId first!");
        }

    }


    public void disconnect() {
        if (idCardController != null) {

            idCardController.close();
        }
        if (bleDevice != null) {

            BleManager.getInstance().disconnect(bleDevice);
        }
    }
}
